package com.keeson.android_cli.util;


import android.content.Context;
import com.keeson.android_cli.R;
import com.keeson.android_cli.util.uifactory.Axis;
import com.wheelpicker.DataPicker;
import com.wheelpicker.DateWheelPicker;
import com.wheelpicker.OnDataPickListener;
import com.wheelpicker.OnDatePickListener;
import com.wheelpicker.PickOption;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class DataPickerUtil {


    public  static final String TIME_YYYY_MM_DD = "yyyy-MM-dd";

    private volatile static DataPickerUtil client ;
    public DataPickerUtil() {
        System.out.println(" created " + getClass().getSimpleName());
    }

    public static DataPickerUtil getInstance() {
        if (client == null)
            synchronized (DataPickerUtil.class) {
                if (client == null) {
                    client = new DataPickerUtil();
                }
            }
        return client;
    }


    /**
     * 年月日
     * @param context
     * @param onDatePickListener
     */
    public void setYearMouthDay(Context context,Date mInitBirthday,OnDatePickListener onDatePickListener){
        PickOption option = getPickDefaultOptionBuilder(context)
                .setMiddleTitleText("")
                .setDateWitchVisible(DateWheelPicker.TYPE_YY_MM_DD)
                .setAheadYears(50)
                .setAfterYears(50)
                .build();
        DataPicker.pickDate(context, mInitBirthday, option, onDatePickListener);
    }




    /**
     * 未来时间
     * @param context
     * @param onDatePickListener
     */
    public void setFutureDate(Context context,int days,Date FutureDate,OnDatePickListener onDatePickListener){
        PickOption option = getPickDefaultOptionBuilder(context)
                .setMiddleTitleText("")
                .setDurationDays(days)
                .build();
        DataPicker.pickFutureDate(context, FutureDate, option, onDatePickListener);
    }





    /**
     * 单行
     * @param mContext
     * @param height
     * @param onDataPickListener
     */
    public void setSingleText(Context mContext, String height, List<String> list, OnDataPickListener<String> onDataPickListener){
        PickOption option = getPickDefaultOptionBuilder(mContext)
                .setMiddleTitleText("")
                .build();
        DataPicker.pickData(mContext,height ,list,option, onDataPickListener);
    }





    /**
     * 格式化时间
     *
     * @param date   需要被处理的日期,距离1970的long
     * @param format 最终返回的日期字符串的格式串
     * @return
     */
    public  String formatDate(long date, String format) {
        DateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public long dateToStamp(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return ts;
    }

    private PickOption.Builder getPickDefaultOptionBuilder(Context context) {
        return PickOption.getPickDefaultOptionBuilder(context)
                .setVisibleItemCount(7)
                .setShadowFactor(0f) //设置滚轮的偏向因子
                .setItemSpace(Axis.scaleX(88)) //设置item的间距
                .setItemTextColor(0xFF000000) //设置item的文本颜色
                .setVerPadding(context.getResources().getDimensionPixelSize(R.dimen.px20)) //设置item的顶部，底部的padding
                .setLeftTitleColor(0xFF999999)
                .setRightTitleColor(0xFF81B1FF)
                .setMiddleTitleColor(0xFF333333)
                .setTitleBackground(0XFFFFFFFF)
                .setBackgroundColor(0xFFFAFAFA)
                .setLeftTitleText("取消")
                .setRightTitleText("确定");
    }


}
